﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

/**
 * 
 * @author 失足程序员
 * @Blog http://www.cnblogs.com/shizuchengxuyuan/
 * @mail 492794628@qq.com
 * @phone 13882122019
 */
namespace Net.Sz.Framework.Utils
{
    /// <summary>
    /// 时间验证器
    /// <para>PS:</para>
    /// <para>@author 失足程序员</para>
    /// <para>@Blog http://www.cnblogs.com/shizuchengxuyuan/</para>
    /// <para>@mail 492794628@qq.com</para>
    /// <para>@phone 13882122019</para>
    /// </summary>
    public class StringUtil
    {
        /// <summary>
        /// 匹配符 ;|；
        /// </summary>
        public static readonly Regex FENHAO_REG = new Regex(";|；");
        /// <summary>
        /// 匹配符 :|：
        /// </summary>
        public static readonly Regex MAOHAO_REG = new Regex(":|：");
        /// <summary>
        /// 匹配符 :|：|=|_
        /// </summary>
        public static readonly Regex MAOHAO_1_REG = new Regex(":|：|=|_");
        /// <summary>
        /// 匹配符 ,|，
        /// </summary>
        public static readonly Regex DOUHAO_REG = new Regex(",|，");
        /// <summary>
        /// 匹配符 /
        /// </summary>
        public static readonly Regex XIEGANG_REG = new Regex("/");
        /// <summary>
        /// 匹配符 |
        /// </summary>
        public static readonly Regex SHUXIAN_REG = new Regex("\\|");
        /// <summary>
        /// 匹配符 _
        /// </summary>
        public static readonly Regex XIAHUAXIAN_REG = new Regex("_");
        /// <summary>
        /// 匹配符 #
        /// </summary>
        public static readonly Regex JINGHAO_REG = new Regex("\\#");
        /// <summary>
        /// 匹配符  =
        /// </summary>
        public static readonly Regex DENGHAO = new Regex("=");
        /// <summary>
        /// 匹配符 @
        /// </summary>
        public static readonly Regex AT_REG = new Regex("@");

        /// <summary>
        /// 验证必须是 数字 or 字母 or 下划线
        /// </summary>
        public static readonly Regex PATTERN_ABC_0 = new Regex("^[_\\s@a-zA-Z0-9]{0,}$");
        /// <summary>
        /// 过滤 非 数字 or 字母 or 下划线
        /// </summary>
        public static readonly Regex PATTERN_REPLACE_ABC_0 = new Regex("[^_\\s@a-zA-Z0-9\\u4e00-\\u9fa5]");
        /// <summary>
        /// 验证必须是 数字 or 字母 or 下划线
        /// </summary>
        public static readonly Regex PATTERN_ABC_PWD = new Regex("^[~\"\"?<>\\[\\]={}_\\-*&()!:@$%\\^.\\s@a-zA-Z0-9]{0,}$");
        /// <summary>
        /// 验证只能是,汉字，数字，字母，下划线
        /// </summary>
        public static readonly Regex PATTERN_ABC_1 = new Regex("^[_\\s@a-zA-Z0-9\\u4e00-\\u9fa5]{0,}$");
        /// <summary>
        /// 验证只能是,汉字，数字，字母
        /// </summary>
        public static readonly Regex PATTERN_ABC_2 = new Regex("^[\\s@a-zA-Z0-9\\u4e00-\\u9fa5]{0,}$");
        /// <summary>
        /// 过滤 非 汉字，数字，字母，下划线
        /// </summary>
        public static readonly Regex PATTERN_REPLACE_ABC_1 = new Regex("[^_\\s@a-zA-Z0-9\\u4e00-\\u9fa5]");
        /// <summary>
        /// 验证必须是字母开头
        /// </summary>
        public static readonly Regex PATTERN_ABC = new Regex("^[a-zA-Z]\\S+$");

        /// <summary>
        /// 纯汉字
        /// </summary>
        public static readonly Regex PATTERN_UUU = new Regex("^[\\u4e00-\\u9fa5]{0,}$");
        /// <summary>
        /// 过滤 非 纯汉字
        /// </summary>
        public static readonly Regex PATTERN_REPLACE_UUU = new Regex("[^\\u4e00-\\u9fa5]");

        /// <summary>
        /// 汉字，字母，数字，以及一些常规字符
        /// </summary>
        public static readonly Regex PATTERN_ABC_00_UUU = new Regex("^[~`“”\"\"?<>\\[\\]【】{}_\\-——=《》*&（）()!！:：#@$￥%……\\^.。,，\\s@a-zA-Z0-9\\u4e00-\\u9fa5]{0,}$");
        /// <summary>
        /// 过滤 非 汉字，字母，数字，以及一些常规字符
        /// </summary>
        public static readonly Regex PATTERN_REPLACE_ABC_00_UUU = new Regex("[^~`“”\"\"?<>\\[\\]【】{}_=\\-——《》*&（）()!！:：#@$￥%……\\^.。,，\\s@a-zA-Z0-9\\u4e00-\\u9fa5]");

        /// <summary>
        /// 判断检查
        /// </summary>
        /// <param name="str"></param>
        /// <param name="regx"></param>
        /// <returns></returns>
        public static bool CheckRegex(String str, Regex regx)
        {
            return regx.Match(str).Success;
        }

        /// <summary>
        /// 过滤特殊字符
        /// </summary>
        /// <param name="str"></param>
        /// <param name="regx"></param>
        /// <returns></returns>
        public static String ReplaceRegex(String str, Regex regx)
        {
            return regx.Replace(str, "").Trim();
        }

        /// <summary>
        /// 过滤特殊字符
        /// </summary>
        /// <param name="str"></param>
        /// <param name="regx"></param>
        /// <returns></returns>
        public static String[] SplitRegex(String str, Regex regx)
        {
            return regx.Split(str);
        }

        /// <summary>
        /// 编码 base64
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static String ConvertToBase64String(String str)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(str);
            return Convert.ToBase64String(bytes);
        }

        /// <summary>
        /// 编码 base64
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static String ConvertToBase64String(byte[] bytes)
        {
            return Convert.ToBase64String(bytes);
        }


        /// <summary>
        /// 编码 base64
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static byte[] ConvertToBase64Byte(String str)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(str);
            return Encoding.UTF8.GetBytes(Convert.ToBase64String(bytes));
        }

        /// <summary>
        /// 编码 base64
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static byte[] ConvertToBase64Byte(byte[] bytes)
        {
            return Encoding.UTF8.GetBytes(Convert.ToBase64String(bytes));
        }

        /// <summary>
        /// 编码 base64
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static char[] ConvertToBase64Char(String str)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(str);
            long arrayLength = (long)((4.0d / 3.0d) * bytes.Length);
            // 如果数组长度不可被4整除，则增加.
            if (arrayLength % 4 != 0)
            {
                arrayLength += 4 - arrayLength % 4;
            }
            char[] base64CharArray = new char[arrayLength];
            Convert.ToBase64CharArray(bytes, 0, bytes.Length, base64CharArray, 0);
            return base64CharArray;
        }

        /// <summary>
        /// 编码 base64
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static char[] ConvertToBase64Char(byte[] bytes)
        {
            long arrayLength = (long)((4.0d / 3.0d) * bytes.Length);
            // 如果数组长度不可被4整除，则增加.
            if (arrayLength % 4 != 0)
            {
                arrayLength += 4 - arrayLength % 4;
            }
            char[] base64CharArray = new char[arrayLength];
            Convert.ToBase64CharArray(bytes, 0, bytes.Length, base64CharArray, 0);
            return base64CharArray;
        }


        /// <summary>
        /// 反解 base64
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static String ConvertFromBase64String(String str)
        {
            byte[] bytes = Convert.FromBase64String(str);
            return Encoding.UTF8.GetString(bytes);
        }

        /// <summary>
        /// 反解 base64
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static String ConvertFromBase64String(byte[] bytes)
        {
            string str = Encoding.UTF8.GetString(bytes);
            byte[] baseBytes = Convert.FromBase64String(str);
            return Encoding.UTF8.GetString(bytes);
        }


        /// <summary>
        /// 反解 base64
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static byte[] ConvertFromBase64Byte(String str)
        {
            return Convert.FromBase64String(str);
        }

        /// <summary>
        /// 反解 base64
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static byte[] ConvertFromBase64Byte(byte[] bytes)
        {
            string str = Encoding.UTF8.GetString(bytes);
            return Convert.FromBase64String(str);
        }

        /// <summary>
        /// 反解 base64
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static byte[] ConvertFromBase64Char(char[] chars)
        {
            return Convert.FromBase64CharArray(chars, 0, chars.Length);
        }

        /// <summary>
        /// 反解 base64
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static String ConvertFromBase64CharString(char[] chars)
        {
            return Encoding.UTF8.GetString(ConvertFromBase64Char(chars));
        }

        /// <summary>
        /// AES 对称加密
        /// <para>先编码 base64 位字符串,再加密</para>
        /// </summary>
        /// <param name="str"></param>
        /// <param name="kk"></param>
        /// <returns></returns>
        public static String Convert_InBase64_AES(String str, params int[] kk)
        {
            char[] strChars = ConvertToBase64Char(str);
            return Convert_AES_ToString(strChars, kk);
        }


        /// <summary>
        /// AES 对称解密
        /// <para>先解密，然后再 base64 解码</para>
        /// </summary>
        /// <param name="str"></param>
        /// <param name="kk"></param>
        /// <returns></returns>
        public static String Convert_UnBase64_AES(String str, params int[] kk)
        {
            char[] strChars = Convert_AES_ToChar(str, kk);
            return ConvertFromBase64CharString(strChars);
        }

        /// <summary>
        /// AES 对称加密
        /// </summary>
        /// <param name="str"></param>
        /// <param name="kk"></param>
        /// <returns></returns>
        public static String Convert_AES(String str, params int[] kk)
        {
            char[] strChars = str.ToCharArray();
            return Convert_AES_ToString(strChars, kk);
        }

        /// <summary>
        /// AES 对称加密
        /// </summary>
        /// <param name="str"></param>
        /// <param name="kk"></param>
        /// <returns></returns>
        public static char[] Convert_AES_ToChar(String str, params int[] kk)
        {
            char[] strChars = str.ToCharArray();
            return Convert_AES_ToChar(strChars, kk);
        }

        /// <summary>
        /// AES 对称加密
        /// </summary>
        /// <param name="str"></param>
        /// <param name="kk"></param>
        /// <returns></returns>
        public static String Convert_AES_ToString(char[] strChars, params int[] kk)
        {
            return new String(Convert_AES_ToChar(strChars, kk));
        }

        /// <summary>
        /// AES 对称加密
        /// </summary>
        /// <param name="str"></param>
        /// <param name="kk"></param>
        /// <returns></returns>
        public static char[] Convert_AES_ToChar(char[] strChars, params int[] kk)
        {
            /*二分对称性*/
            int fcount = strChars.Length / 2;
            /*对称性 k 值*/
            foreach (int k in kk)
            {
                /*间隔一个字符，替换*/
                for (int i = 0; i < fcount; i += k)
                {
                    /*对称处理*/
                    char tmp = strChars[i];
                    strChars[i] = strChars[fcount + i];
                    strChars[fcount + i] = tmp;
                }
            }
            return strChars;
        }
    }
}
